
//
//  VideoPlayerMaskView.swift
//  MyLibrary
//
//  Created by BO ZHOU on 2020/9/15.
//  Copyright © 2020 ncit. All rights reserved.
//

import UIKit



/** 视频播放头部显示视频信息的蒙版view, 用来实现label边缘淡化功能 */
class ZBGradientMaskView: UIView {
    
    /**
    style

    - topTobottom:  从上到下的颜色
    - bottomTotop:  从下到上的颜色
    */
    enum MaskStyle {
        case topTobottom
        case bottomTotop
    }
    
    var style: MaskStyle!

    /** 修改自己的layer为gradientLayer */
    override class var layerClass: AnyClass {
        return CAGradientLayer.self
    }
    
    
    /**
     * 初始化
     * - parameter colors: 渐变颜色, 只支持两色
     * - parameter style: topTobottom--从上到下(默认), bottomTotop--从下到上
     */
    init(colors: [CGColor]? = nil, style: MaskStyle = .topTobottom) {
        
        super.init(frame: .zero)
        self.style = style
        
        let layer = self.layer as! CAGradientLayer
        switch style {
        case .topTobottom:
            
            if let gcolors = colors {
                layer.colors = gcolors
            } else {
                layer.colors = [UIColor(white: 0, alpha: 0.6).cgColor, UIColor.clear.cgColor]
            }
        case .bottomTotop:
            if let gcolors = colors {
                layer.colors = gcolors
            } else {
                layer.colors = [UIColor.clear.cgColor, UIColor(white: 0, alpha: 0.6).cgColor]
            }
        }
    }
        
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /** 清除颜色, 设置colors为nil, 整个maskView控件就不会展示了 */
    func cleanColors() {
        let layer = self.layer as! CAGradientLayer
        layer.colors = nil
    }
}
    

